﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Xml;
using System.Text;
using System.Threading.Tasks;
using SupermarketModel;
using Supermarket.Data;
using System.Data;
using System.IO;

namespace Supermarket.Client
{
    static class DataImporter
    {
        public static void ImportProducts()
        {
            using (SupermarketEntities MySQLDatabase = new SupermarketEntities())
            {
                using (SupermarketContext SQLServerDatabase = new SupermarketContext())
                {
                    var vendors = MySQLDatabase.Vendors;
                    foreach (var vendor in vendors)
                    {
                        var newVendor = new Vendor();
                        newVendor.Name = vendor.Name;
                        SQLServerDatabase.Vendors.Add(newVendor);
                    }

                    var measures = MySQLDatabase.Measures;
                    foreach (var measure in measures)
                    {
                        var newMeasure = new Measure();
                        newMeasure.Name = measure.Name;
                        SQLServerDatabase.Measures.Add(newMeasure);
                    }
                    SQLServerDatabase.SaveChanges();

                    var products = MySQLDatabase.Products;
                    foreach (var product in products)
                    {
                        var newProduct = new Product();
                        newProduct.BasePrice = product.BasePrice;
                        newProduct.MeasureID = product.MeasureID;
                        newProduct.Name = product.Name;
                        newProduct.VendorID = product.VendorID;
                        SQLServerDatabase.Products.Add(newProduct);
                    }
                    SQLServerDatabase.SaveChanges();
                }
            }
        }

        public static void ImportVendorExpenses()
        {
            using (SupermarketContext database = new SupermarketContext())
            {
                using (XmlReader reader = XmlReader.Create("../../Vendors-Expenses.xml"))
                {
                    IList<Vendor> AllVendors = database.Vendors.ToList();
                    Vendor currentVendor = null;
                    

                    while (reader.Read())
                    {
                        if (currentVendor != null && reader.NodeType == XmlNodeType.Element && reader.Name == "expenses")
                        {
                            VendorExpenses vendorExpenses = new VendorExpenses()
                            {
                                Vendor = currentVendor,
                                Month = DateTime.Parse(reader["month"]),
                                Expenses = decimal.Parse(reader.ReadElementString(), CultureInfo.InvariantCulture)
                            };
                            database.VendorExpenses.Add(vendorExpenses);
                        }
                        else if ((reader.NodeType == XmlNodeType.Element) && (reader.Name == "sale"))
                        {
                            currentVendor = AllVendors.Where(v => v.Name.Equals(reader["vendor"])).FirstOrDefault();
                        }

                        database.SaveChanges();
                    }
                }
            }
        }

        public static void ImportSales()
        {
            ExcelFromZip.ImportSales();
        }
    }
}
